import java.util.Arrays;

    public  class MyArraylist implements IArrayList{

    public int[] elem;
    public int usedSize;//0
    //默认容量
    private static final int DEFAULT_SIZE = 10;

    public MyArraylist() {
        this.elem = new int[DEFAULT_SIZE];
    }

    public MyArraylist(int capacity) {
        this.elem = new int[capacity];
    }



    /**
     * 打印顺序表:
     * 根据usedSize判断即可
     */
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.println(i+' ');
        }
    }

    @Override
    public String toString() {
        return "MyArraylist{" +
                "elem=" + Arrays.toString(elem) +
                '}';
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        checkCapicity();
        elem[usedSize]=data;
        usedSize++;
    }




    private void checkCapicity() {
        if(isFull()) {
            this.elem = Arrays.copyOf(this.elem, this.elem.length * 2);
        }
    }
    /**
     * 判断当前的顺序表是不是满的！
     *
     * @return true:满   false代表空
     */
       public boolean isFull() {
       return usedSize==elem.length;

    }

    private void checkPosInAddOrGet(int pos)throws PosIllegal {
        if(pos>=0&&pos<=usedSize) {
            System.out.println("下标"+pos+"位置插入成功");
        } else {
            System.out.println("下标不合法!" );
            throw new PosIllegal("插入下标元素异常:"+pos);
        }

    }

    // 在 pos 位置新增元素
    public void add(int pos, int data)throws PosIllegal {
        try {
            checkPosInAddOrGet(pos);
        }catch (PosIllegal e){
            e.printStackTrace();
        }
        checkPosInAddOrGet(pos);
        this.elem[pos]=data;
        usedSize++;
}
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(this.elem[i]==toFind)
                return  true;
        }
        return false;
}

    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (this.elem[i]==toFind) {
                return i;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素
    public int get(int pos) throws PosIllegal{
        try {
            checkPosInAddOrGet(pos);
        }catch (PosIllegal e){
            e.printStackTrace();
        }
        return this.elem[pos];
    }

    public boolean isEmpty() {
        if(usedSize==0)
            return true;
        return false;
    }

    // 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, int value) throws PosIllegal{
        try {
            checkPosInAddOrGet(pos);
        }catch (PosIllegal e) {
            e.printStackTrace();
        }
        this.elem[pos]=value;
    }

    /**
     * 删除第一次出现的关键字key
     *
     * @param key
     */
    public void remove(int key){
        int removePos=indexOf(key);
        if (removePos==-1)
            System.out.println("删除失败,不存在关键字:"+key);
        else {
            for (int i = removePos; i <usedSize ; i++) {
                this.elem[removePos]=this.elem[removePos+1];
            }
            usedSize--;
        }
    }

    // 获取顺序表长度
    public int size() {
        return usedSize;
    }

    // 清空顺序表
    public void clear() {
        for (int i = 0; i < usedSize; i++) {
            this.elem[i]=0;
        }
    }


}

